<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        // map() 返回一个新的数组，由原来的数组中的每个元素都调用一次提供的方法后的返回值组成

        /* 
            需求：将数组的元素每个值都x2
        */
        const array1 = [1,2,3,4,5,6];

        // 方式一
        for(var i = 0;i < array1.length; i++){
            array1[i] *= 2;
        }
        console.log(array1);
        console.log("=================");

        // 方式二
        function add(num){
            return num * 2;
        }

        console.log(array1.map(add));
        console.log("=================");

        // 方式三
        const result1 = array1.map(item => item * 2);
        console.log(result1);
        console.log("=================");

         /* 
            const names = [
            //  {name:"张三",age:22},
             {name:"李四",age:23},
             {name:"赵六",age:24},
             {name:"张三",age:32}

             需求：将每个人的age+1
         ];
         */
        const names = [
             {name:"李四",age:23},
             {name:"赵六",age:24},
             {name:"张三",age:32}
         ];

         const result = names.map(obj => obj.age+=1);
         console.log(result);//依据返回值生成的新数组，所以只包含年龄，而不是整个对象
         /* 
            匿名函数：obj => obj.age+1
            function (obj) {
                return obj.age+1;
            }
          */

    </script>
</head>
<body>
    
</body>
</html>